package vistop;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import vistop.datatypes.Data;
import vistop.datatypes.Face;
import vistop.datatypes.Polygon;
import vistop.io.lisp.LispPolygonFactory;
import vistop.ui.UserInterface;

public class Main {
	public static void main (String[] args) {
		
		List<Polygon> polygons = fillPolygons();
		UserInterface ui = new UserInterface(polygons);
		ui.show();
	}
	
	public static List<Polygon> fillPolygons() {
		List<Polygon> polygons = new ArrayList<Polygon>();
		try {
			FileInputStream fstream = new FileInputStream("cube.lisp");
			InputStream data = new DataInputStream(fstream);
			LispPolygonFactory lpf = new LispPolygonFactory();
			Data d = lpf.createData(data);
			for (Entry<Integer, Face> f : d.getFaces().entrySet()) {
				try {
					Polygon p = new Polygon(f.getValue().getEdges());
					polygons.add(p);
				} catch (IllegalArgumentException e) {
					// TODO: handle exception
					System.out.println("Problem occured with face id " + 
							f.getValue().getId());
					System.out.println(e.toString());
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
			
		return polygons;
	}
	
	@SuppressWarnings("unused")
	private static void printPolygons(List<Polygon> lp) {
		System.out.println("Polygons:");
		for (Polygon p : lp) {
			System.out.println("next face:");
			try {
				p.printComponents(p.getPolygonComponentsVertex());
			} catch (Exception e) {
				System.out.println(e.toString());
			}
		}
	}
}
